package com.dz.finance.wechat.service.impl;

import com.dz.finance.wechat.entity.MpSubscribeEventReq;
import com.dz.finance.wechat.entity.MpTextEventReq;
import com.dz.finance.wechat.enums.WeChatMessageEnum;
import com.dz.finance.wechat.service.WeChatMessageStrategy;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author: DengHaoQi
 * @date: 2024/6/8 17:38
 * @description:
 */


@Slf4j
@Service
@RequiredArgsConstructor
public class TextMsgStrategy implements WeChatMessageStrategy {

    private final ApplicationContext applicationContext;

    @Override
    public WeChatMessageEnum getTypeMsg() {
        return WeChatMessageEnum.TEXT;
    }

    @Override
    public void publishMessage(Object event) {
        MpTextEventReq mpTextEventReq = (MpTextEventReq) event;
        log.info("消息: {}; 消息推送中.....",mpTextEventReq);
        applicationContext.publishEvent(mpTextEventReq);
        log.info("消息推送成功!");
    }

    @Override
    public MpTextEventReq getCurrent(String xmlContent) throws IOException {
        XmlMapper xmlMapper = new XmlMapper();
        return xmlMapper.readValue(xmlContent, MpTextEventReq.class);
    }
}
